﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;

namespace DevicesInt
{
    public class MySerializer
    {

        public static byte[] SerializeToBytes(object obj)
        {
            var ms = new MemoryStream();
            using (var writer = new BsonWriter(ms))
            {
                var serializer = new JsonSerializer();
                serializer.Serialize(writer, obj);
            }
            return ms.ToArray();
        }

        public static T Deserialize<T>(byte[] data, bool isArray = false)
        {
            T serd;
            var ms = new MemoryStream(data);
            using (var reader = new BsonReader(ms))
            {
                if (isArray)
                    reader.ReadRootValueAsArray = true;
                var serializer = new JsonSerializer();
                serd = serializer.Deserialize<T>(reader);
            }
            return serd;
        }
    }
}
